import { info } from "@/api/info.js";

const user = {
    state:{
        userName:"",
        userId:0,
        menus:[],
        roles:[]
    },
    mutations: {
      SET_ROLES:(state,roles)=>{
        state.roles = roles
      } ,
      SET_MENUS:(state,menus)=>{
        state.menus = menus
      } ,
      SET_USER_ID:(state,userId)=>{
        state.userId = userId
      } ,
      SET_USERNAME:(state,userName)=>{
        state.userName = userName
      } ,
    },
    actions:{
        GetInfo({commit}){
            return new Promise((resolve,reject)=>{
                info().then(response=>{
                    const data = response.data
                    commit('SET_USERNAME',data.username)
                    commit('SET_USER_ID',data.userId)
                    if(data.roles && data.roles.length>0){
                        commit('SET_ROLES', data.roles)
                    }
                    if(data.menus && data.menus.length>0){
                        commit('SET_MENUS', data.menus)
                    }
                    resolve(response)
                }).catch(error => {
                    reject(error)
                  })
            })
        }
    }
}

export default user